<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function tranListToTreeData(list, rootValue) {
        var arr = []
        list.forEach((item) => {
          if (item.pid === rootValue) {
            // 找到之后 就要去找 item 下面有没有子节点
            const children = tranListToTreeData(list, item.id)
            if (children.length) {
              // 如果children的长度大于0 说明找到了子节点
              item.children = children
            }
            arr.push(item) // 将内容加入到数组中
          }
        })
        return arr
      }

      const de = [
        {
          id: '1175310929766055936',
          pid: '',
          companyId: '1',
          name: '总裁办',
          code: 'ZCB',
          managerId: null,
          manager: '孙财',
          introduce: '总裁办公室',
          createTime: null,
        },
        {
          id: '1175311213774962688',
          pid: '',
          companyId: '1',
          name: '行政部',
          code: 'XZB',
          managerId: null,
          manager: '罗晓晓',
          introduce: '1111',
          createTime: null,
        },
        {
          id: '1175311267684352000',
          pid: '',
          companyId: '1',
          name: '人事部',
          code: 'RSB',
          managerId: '1071632760222810112',
          manager: '文吉星',
          introduce: '123',
          createTime: null,
        },
        {
          id: '1175311325720936448',
          pid: '',
          companyId: '1',
          name: '财务部',
          code: '',
          managerId: null,
          manager: null,
          introduce: null,
          createTime: null,
        },
        {
          id: '1175311373083017216',
          pid: '',
          companyId: '1',
          name: '技术部',
          code: 'JSB',
          managerId: null,
          manager: null,
          introduce: null,
          createTime: null,
        },
        {
          id: '1175311418004013056',
          pid: '',
          companyId: '1',
          name: '运营部',
          code: 'YYB',
          managerId: null,
          manager: null,
          introduce: null,
          createTime: null,
        },
        {
          id: '1175311466846683136',
          pid: '',
          companyId: '1',
          name: '市场部',
          code: 'SCB',
          managerId: '1063705989926227968',
          manager: '武高丽',
          introduce: null,
          createTime: null,
        },
        {
          id: '1235395178363559936',
          pid: '1175311325720936448',
          companyId: '1',
          name: '财务核算部',
          code: null,
          managerId: null,
          manager: null,
          introduce: null,
          createTime: null,
        },
        {
          id: '1235398264104624128',
          pid: '1175311325720936448',
          companyId: '1',
          name: '税务管理部',
          code: null,
          managerId: null,
          manager: null,
          introduce: null,
          createTime: null,
        },
        {
          id: '1235398536969265152',
          pid: '1175311325720936448',
          companyId: '1',
          name: '薪资管理部',
          code: null,
          managerId: null,
          manager: null,
          introduce: null,
          createTime: null,
        },
        {
          id: '1235398608847052800',
          pid: '1175311373083017216',
          companyId: '1',
          name: 'Java研发部',
          code: null,
          managerId: null,
          manager: null,
          introduce: null,
          createTime: null,
        },
        {
          id: '1235398661355544576',
          pid: '1175311373083017216',
          companyId: '1',
          name: 'Python研发部',
          code: null,
          managerId: null,
          manager: null,
          introduce: null,
          createTime: null,
        },
        {
          id: '1235398708763762688',
          pid: '1175311373083017216',
          companyId: '1',
          name: 'Php研发部',
          code: null,
          managerId: null,
          manager: null,
          introduce: null,
          createTime: null,
        },
        {
          id: '1235398917619130368',
          pid: '1175311466846683136',
          companyId: '1',
          name: '北京事业部',
          code: null,
          managerId: null,
          manager: null,
          introduce: null,
          createTime: null,
        },
        {
          id: '1235399011458293760',
          pid: '1175311466846683136',
          companyId: '1',
          name: '上海事业部',
          code: null,
          managerId: null,
          manager: null,
          introduce: null,
          createTime: null,
        },
        {
          id: '1561522821536354304',
          pid: '1175311466846683136',
          companyId: '1',
          name: '全部删除了',
          code: 'QBSCL',
          managerId: null,
          manager: '飞飞飞',
          introduce: '哈哈哈哈哈哈',
          createTime: null,
        },
        {
          id: '1561522934887419904',
          pid: '1561522821536354304',
          companyId: '1',
          name: '测试别删',
          code: '123',
          managerId: null,
          manager: '无小黑u',
          introduce: '123',
          createTime: null,
        },
        {
          id: '1561523018098216960',
          pid: '1561522934887419904',
          companyId: '1',
          name: '好的',
          code: 'HD',
          managerId: null,
          manager: '文吉星',
          introduce: '22',
          createTime: null,
        },
        {
          id: '1561534876255457280',
          pid: null,
          companyId: '1',
          name: '12345',
          code: '12345',
          managerId: null,
          manager: '飞飞飞',
          introduce: '12345',
          createTime: null,
        },
        {
          id: '1561534964952403968',
          pid: null,
          companyId: '1',
          name: '12345部门',
          code: '12345',
          managerId: null,
          manager: '飞飞飞',
          introduce: '12345',
          createTime: null,
        },
        {
          id: '1561536538747539456',
          pid: null,
          companyId: '1',
          name: '11111',
          code: '111111',
          managerId: null,
          manager: '阿敏',
          introduce: '11111111',
          createTime: null,
        },
        {
          id: '1561544719158677504',
          pid: '1235399011458293760',
          companyId: '1',
          name: '马冬梅',
          code: '1234',
          managerId: null,
          manager: '老大爷',
          introduce: '什么都没',
          createTime: null,
        },
        {
          id: '1561544733767438336',
          pid: '',
          companyId: '1',
          name: '11111',
          code: '1422424',
          managerId: null,
          manager: '菜虚坤',
          introduce: '2424224',
          createTime: null,
        },
        {
          id: '1561545038303268864',
          pid: '',
          companyId: '1',
          name: '21212',
          code: '121212',
          managerId: null,
          manager: '菜虚坤',
          introduce: '12212121',
          createTime: null,
        },
      ]
      const res = tranListToTreeData(de, '')
      console.log('REs:', res)
    </script>
  </body>
</html>
